<!DOCTYPE html>
<html lang="en">

	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
		<meta name="description" content="">
		<meta name="author" content="">
		<!-- STYLESHEETS -->
		<!--[if lt IE 9]><script src="../../js/flot/excanvas.min.js"></script><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script><![endif]-->
		<link rel="stylesheet" type="text/css" href="../../css/cloud-admin.css">
		<link rel="stylesheet" type="text/css" href="../../css/themes/default.css" id="skin-switcher">
		<link rel="stylesheet" type="text/css" href="../../css/responsive.css">
				<link href="../../font-awesome/css/font-awesome.min.css" rel="stylesheet">
		<!-- file-input -->
		<link rel="stylesheet" type="text/css" href="../../js/file-input/fileinput.css" />
		<!-- datatable -->
		<link rel="stylesheet" href="../../js/datatables/datatable.css">
		<!-- WIZARD -->
		<link rel="stylesheet" type="text/css" href="../../js/bootstrap-wizard/wizard.css" />

	</head>

	<body>
		<header class="navbar clearfix" id="header">
		</header>

		<!-- PAGE -->
		<section id="page">
			<!-- SIDEBAR -->
			<div id="sidebar" class="sidebar">
			<div class="sidebar-menu nav-collapse">
						
						<!-- SIDEBAR MENU -->
						<ul>							
							
							
						</ul>
						<!-- /SIDEBAR MENU -->
					</div>

			</div>
			<!-- /SIDEBAR -->
			<div id="main-content">
			<!-- SAMPLE BOX CONFIGURATION MODAL FORM-->
				<div class="modal fade" id="order_config" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
					<div class="modal-dialog">
						<div class="modal-content">
							<div class="modal-header">
								<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
								<h4 class="modal-title">订单发货状态确认</h4>
							</div>
							<div class="modal-body">
								<div class="form-group">
								<label class="control-label col-md-3" style="line-height:34px;">您已经确认过订单的发货状态？</label>
								</div>
							</div>
							
							<div class="modal-footer">
								<button type="button" class="btn btn-default" data-dismiss="modal">未发货</button>
								<button type="button" class="btn btn-success bt-save">已发货</button>
							</div>
						</div>
					</div>
				</div>
				<div class="container">
					<div class="row">
						<div id="content" class="col-lg-12">
							<!-- PAGE HEADER-->
							<div class="row">
								<div class="col-sm-12">
									<div class="page-header">
										<!-- STYLER -->

										<!-- /STYLER -->
										<!-- BREADCRUMBS -->
										<ul class="breadcrumb">
											<li>
												<i class="fa fa-home"></i>
												<a href="index.html">首页</a>
											</li>
											<li>
												<a href="#">订单管理</a>
											</li>
											<li>订单详情</li>
										</ul>
										<!-- /BREADCRUMBS -->

									</div>
								</div>
							</div>
							<!-- /PAGE HEADER -->
							<!-- SAMPLE -->

							<div class="row">
								<div class="col-md-12" style="background-color:#ffffff">
									<div class="box" style="margin-top:10px">
										<div class="box-title" style="border-bottom:1px solid #5e87b0;line-height:38px">
											<h4><i class="fa fa-file" style="color:red"></i>基本信息</h4>
											<a class="btn btn-default" href="../../pages/returnorder/return_order_list.html" style="float:right">返回订单列表</a>
										</div>
										<div class="box-body">
											<div class="form-group  mrg-bt10">
												<label class="control-label col-md-2 " style="width:100px">订单编号：</label>
												<div class="col-md-2 inline" id="orderId" style="width:110px">
												</div>
												<label class="control-label col-md-2 " style="width:70px">状态：</label>
												<div class="col-md-2 inline" id="orderStatus" style="width:110px">
												</div>
												<label class="control-label col-md-2 " style="width:120px">订单总金额：</label>
												<div class="col-md-2 inline" id="totalPrice" style="width:110px">
												</div>												
											    <label class="control-label col-md-2 " style="width:120px">已退款金额：</label>
													<div class="col-md-2 inline" id="hasReturnPrice" style="width:110px">
												</div>
											    <label class="control-label col-md-2 " style="width:130px">已退成长基金：</label>
													<div class="col-md-2 inline" id="hasReturnAccountPrice" style="width:110px">
												</div>
											</div>
											<div class="form-group  mrg-bt10">
													<label class="col-sm-1 control-label" style="width:100px">申请原因：</label>
													<div class="col-sm-2">
														<select id="returnReasonType" name="returnReasonType" class="col-md-12 select2-offscreen form-control"
															tabindex="-1">
															<option value="">请选择申请原因</option>
															<option value="WithoutReason">七天无理由</option>
															<option value="ReturnDifference">退差价</option>
															<option value="OutPreSale">超出预售期，不想等了</option>
															<option value="SizeNotMatch">尺码与描述不符</option>
															<option value="LessMissWrong">少发/漏发/错发</option>
															<option value="BadWork">做工不好</option>
															<option value="QualityIssues">质量问题</option>
															<option value="Other">其他</option>
														</select>
													</div>
											</div>
										</div>
									</div>
									<div class="box div_change" style="display:none">
										<div class="box-title" style="border-bottom:1px solid #5e87b0">
											<h4><i class="fa fa-user" style="color:#eea236"></i>收货信息</h4>
										</div>
										<div class="box-body">
											<div class="form-group mrg-bt10">
												<label class="control-label col-md-2" style="width:100px">收货人：</label>
												<div class="col-md-2 inline" id="receiver">
												</div>
												<label class="control-label col-md-2" style="width:100px">电话：</label>
												<div class="col-md-2 inline" id="telPhone">
												</div>
												<label class="control-label col-md-2" style="width:100px">地址：</label>
												<div class="col-md-3 inline" id="address">
												</div>
											</div>
											
										</div>
									</div>
									<div class="box div_sourceproduct">
										<div class="box-title" style="border-bottom:1px solid #5e87b0">
											<h4><i class="fa fa-archive" style="color:#468847"></i>原商品信息</h4>
										</div>
										<div class="box-body">
											<div class="form-group" style="height:auto;">
												<table id="source_sku_list" class="table table-bordered">
													<thead>
													<tr>
														<th style="width:20%">商品名称</th>
														<th style="width:10%">SKU编码</th>
														<th style="width:10%">颜色/规格</th>
														<th style="width:10%">尺码</th>
														<th style="width:10%">单价</th>
														<th style="width:5%">剩余数量</th>
														<th style="width:35%">操作</th>
																				
														</tr>
													</thead>
													<tbody>														
													</tbody>
												</table>
											</div>
										</div>
									</div>
									<div class="box div_product">
										<div class="box-title" style="border-bottom:1px solid #5e87b0">
											<h4><i class="fa fa-archive" style="color:#468847"></i>退换商品信息</h4>
										</div>
										<div class="box-body">
											<div class="form-group" style="height:auto;">
												<table id="sku_list" class="table table-bordered">
													<thead>
													<tr>
														<th style="width:20%">商品名称</th>
														<th style="width:10%">SKU编码</th>
														<th style="width:10%">颜色/规格</th>
														<th style="width:10%">尺码</th>
														<th style="width:10%">单价</th>
														<th style="width:10%">数量</th>
														<th style="width:5%">退换原因</th>														
														<th style="width:35%">操作</th>														
													</tr>
													</thead>
													<tbody>														
													</tbody>
												</table>
											</div>
										</div>
									</div>
									<div class="box" style="display:">
										<div class="box-title" style="border-bottom:1px solid #5e87b0">
											<h4><i class="fa fa-info-circle" style="color:#d9edf7"></i>退款信息</h4>
										</div>
										<div class="box-body">
											<div class="form-group  mrg-bt10">	
												<label class="control-label col-md-2" style="width:100px">退款总额：</label>
													<div class="col-md-2 inline" id="totalRefund" style="width:120px">
												</div>
												<label class="control-label col-md-2" style="width:100px">应退金额：</label>
													<div class="col-md-2 inline" id="price" style="width:120px">
												</div>
												<label class="control-label col-md-2"  style="width:130px">应退成长基金：</label>
													<div class="col-md-2 inline" id="accountPrice" style="width:120px">
												</div>
												<div class="form-group  mrg-bt10">	
												<label class="control-label col-md-2" style="width:100px">实退金额：</label>
													<div class="col-md-2 inline" style="width:150px">
															<input type="text" id="refundPrice" class="form-control" placeholder="应退金额" onkeypress="return $.IsDecimal(event);" onblur="return $.IsMoney(this);">
												</div>
												<div class="form-group  mrg-bt10">	
												<label class="control-label col-md-2" style="width:130px">实退成长基金：</label>
													<div class="col-md-2 inline" style="width:150px">
															<input type="text" id="refundAccountPrice" class="form-control" placeholder="应退成长基金" onkeypress="return $.IsDecimal(event);" onblur="return $.IsMoney(this);">
												</div>
											</div>
										</div>
									</div>
									<div class="box" style="display:none">
										<div class="box-title" style="border-bottom:1px solid #5e87b0">
											<h4><i class="fa fa-info-circle" style="color:#d9edf7"></i>问题信息</h4>
										</div>
										<div class="box-body">
											<div class="form-group  mrg-bt10 returnReason" style="color: red;font-weight: bold">
												
											</div>
											<div class="form-group  mrg-bt10 returnImgPath" style="display:inline">
												
												
											</div>
										</div>
									</div>
									<div class="box div_change" style="display:">
										<div class="box-title" style="border-bottom:1px solid #5e87b0">
											<h4><i class="fa fa-truck" style="color:#d9edf7"></i>物流信息</h4>
										</div>
									</div>
									<div class="box-body big" style="padding:10px;">												
											
											<label class="control-label col-md-1" style="line-height:34px;"><span class="required"></span>物流公司：</label>
											<div class="col-md-2">
												<select  id="userexpress_select" class="col-md-12 select2-offscreen form-control" tabindex="-1">
													<option value='STO'>申通快递</option>
													<option value='ZTO'>中通快递</option>
													<option value='YTO'>圆通速递</option>
													<option value='SF'>顺丰速运</option>
													<option value='YD'>韵达速递</option>
												</select>											
											</div>
											<label class="control-label col-md-2" style="line-height:34px;"><span class="required"></span>物流单号：</label>
											<div class="col-md-3">
												<input type="text" class="form-control" name="userexpressCode" placeholder="请输入物流单号" maxlength="20" />
												
											</div>
									</div>
									<div class="box div_returnlog" style="display:none">
										<div class="box-title" style="border-bottom:1px solid #5e87b0">
											<h4><i class="fa fa-smile-o" style="color:#fcd76a"></i>进度跟踪</h4>
										</div>
										<div class="box-body">
											<table id="log_list" class="table table-bordered">
													<thead>
													<tr>
														<th style="width:15%">操作时间</th>
														<th style="width:55%">描述</th>
														<th style="width:20%">备注</th>
														<th style="width:10%">操作人</th>
																											
														</tr>
													</thead>
													<tbody>														
													</tbody>
												</table>
										</div>
									</div>
									<div style="display:none">
										<div class="box div_yijian" >
											<div class="box-title" style="border-bottom:1px solid #5e87b0">
												<h4><i class="fa fa-smile-o" style="color:#fcd76a"></i>审批意见</h4>
											</div>
											<div class="box-body">
												<textarea name="remark" placeholder="请输入审批意见" class="form-control" style="width:280px;resize:none;" ></textarea>
											</div>
										</div>
										<div class="box div_yijian">
											<button type="button" class="btn btn-danger" status="reject">拒绝</button>
											<button type="button" class="btn btn-success" status="pass">通过</button>
										</div>
									</div>
									<div>
										<div class="box div_yijian" style="width: 82px;margin: 0 auto;padding-bottom: 40px;">
											<button type="button" class="btn btn-danger saveReturn" >确认退货</button>
										</div>
									</div>
								</div>
							</div>

							<!-- /SAMPLE -->

							<div class="footer-tools">
								<span class="go-top">
								<i class="fa fa-chevron-up"></i> Top
							</span>
							</div>
						</div>
						<!-- /CONTENT-->
					</div>
				</div>
			</div>
		</section>
		<!--/PAGE -->
		<!-- JAVASCRIPTS -->
		<!-- Placed at the end of the document so the pages load faster -->
		<!-- JQUERY -->
		<script src="../../js/jquery/jquery-2.0.3.min.js"></script>

		<!-- BOOTSTRAP -->
		<script src="../../bootstrap-dist/js/bootstrap.min.js"></script>
			<!--bootbox-->
		<script type="text/javascript" src="../../js/bootbox/bootbox.min.js"></script>
		<!-- COOKIE -->
		<script type="text/javascript" src="../../js/jQuery-Cookie/jquery.cookie.min.js"></script>
		<!-- CUSTOM SCRIPT -->
		<script src="../../js/script.js"></script>
		<script src="../../js/common.js"></script>

		<script>
			var AddDetail = function() {
				var btnFlag=false;
				 var returninfo ={};
				 returninfo.skuInfos =[];
				var orderId,returnType;
				var order,returnOrder;
			    var getStatusDesc=function(returOrderStatus){
				       var str="";
									   switch(returOrderStatus){
									     case "TradeClosed":str="交易关闭";break;
									     case "WaitingPay":str="待付款";break;
									     case "WaitingSend":str="待发货";break;
									     case "WaitingSign":str="待签收";break;
									     case "TradeSuccess":str="已完成";break;
									   }
									   return str;
					};
					
					var returnGoods = function(){
						
						 var tr = $(this).parent().parent();
						 var res = checkQuantity(tr);
						 
						 if(res){
							 var skuId = this.id;
							 var orderDetailId = $(this).attr('orderDetailId');
							 var productName = $(tr).find('td:eq(0)').html();
							 var skuCode = $(tr).find('td:eq(1)').html();
							 var imgpath = $(tr).find('td:eq(1)').attr("imgpath");
							 var shortName = $(tr).find('td:eq(2)').html();
							 var size = $(tr).find('td:eq(3)').html();
							 var salePrice = $(tr).find('td:eq(4)').html();
							 var quantity = $(tr).find('td:eq(5)').find(".returnquantity").val();
							 var max = $(tr).find('td:eq(5)').find(".returnquantity").attr("max");
							 $(tr).find('td:eq(5)').find(".returnquantity").val(max-quantity)
							 
							 var str="<tr><td>"+productName+"</td>"
						     	+"<td  imgpath='"+imgpath+"' skuid='"+skuId+"' orderDetailId='"+orderDetailId+"'>"+skuCode+"</td>"
						     	+"<td>"+shortName+"</td>"
						     	+"<td>"+size+"</td>"
						     	+"<td>"+salePrice+"</td>"
						     	+"<td class='returnquantity'>"+quantity+"</td>"
						     	+"<td>"+"<input class='retrunRemark' type='text' value=''>"+"</td>"
						     	+"<td>"+"<button type='button' skuid='"+skuId+"' class='btn btn-success cancelReturn' >取消</button>"+"</td></tr>";	
							   $("#sku_list tbody").append(str);
							   
							    
							  //    $("#sku_list tbody tr").find(".retrunRemark").unbind("blur").blur(function(){
								// 		var remark = this.value ;
								// 		if(remark ==""){
								// 			$.alert("请输入退货原因。")
								// 		}
								//  });
							     
							     var sku ={};
							     sku.skuId= skuId;
							     sku.quantity=quantity;
							     returninfo.skuInfos.push(sku);
							   $(this).attr("disabled",true)
							  $(tr).find("input").attr('readonly',true);
							   
							   //组装退货请求对象
							   returninfo.orderId=orderId;
							   getReturnPrice(returninfo);
						 }
						 
						
					}
					
					//获取退款金额
					var getReturnPrice =function(){
							var skuInfos =[];
							var returns = $("#sku_list tbody tr");
							$.each(returns,function(index,item){
								var sku ={};
								var skuId = $(item).find("td:eq(1)").attr("skuid");
								var quantity = $(item).find("td:eq(5)").html();
								var orderDetailId = $(item).find("td:eq(1)").attr("orderDetailId");
								sku.skuId = skuId;
								sku.quantity = quantity;
								sku.orderDetailId = orderDetailId
								skuInfos.push(sku);
							})
							returninfo.skuInfos=skuInfos;
						  
						   $.ajax({
							   type:"get",
							   url:App.getContextPath() + "admin/returnorderoff/getReturnPrice.do",
							   data:{
								   returnInfo:JSON.stringify(returninfo)
							   },
							   success:function(result){
								   if(result.type=='success'){
										 $("#totalRefund").html(format_thousands(result.extra.returnPriceModel.returnPrice));
										 $("#price").html(format_thousands(result.extra.returnPriceModel.returnProductPrice));
										 $("#refundPrice").val(result.extra.returnPriceModel.returnProductPrice);
										 $("#accountPrice").html(format_thousands(result.extra.returnPriceModel.returnAccountPrice));
									   $("#refundAccountPrice").val(result.extra.returnPriceModel.returnAccountPrice);
								   }
									   
							   }
						   })
					}
					
					//取消退货
					var cancelReturn = function(){
						var returnquantity = $(this).parent().parent().find("td:eq(5)").html();
						var skuId= $(this).attr("skuid");
						 $("#"+skuId).attr("disabled",false)
						  $(".returnquantity").attr("readonly",false)
						$(this).parent().parent().remove();
						var skuList = $("#source_sku_list tbody .returnquantity");
						$.map(skuList, function(n, i){ 
							if($(n).attr("skuId")== skuId){
								var quantity = $(n).val();
								$(n).val(parseInt(quantity)+parseInt(returnquantity))
							}
							
					    }); 
						 getReturnPrice();
					}
					
					//保存退货
					var saveReturn = function(){
						var expressCompany = $("#userexpress_select").val();
						var expressCode = $("input[name=userexpressCode]").val();
						var skuInfos =[];
						var returnReason="";
						var returns = $("#sku_list tbody tr");
						var imgPath="" ;
						$.each(returns,function(index,item){
							var sku ={};
							var skuId = $(item).find("td:eq(1)").attr("skuid");
							var quantity = $(item).find("td:eq(5)").html();
							var remark = $(item).find("td:eq(6) input").val();
							
							sku.skuId = skuId;
							sku.quantity = quantity;
							// if(remark ==""){
							// 	$.alert( "请填写退货原因");
							// 	return false
							// }
							returnReason =returnReason+remark+";";
							if(index==0){
							 	imgPath = $(item).find("td:eq(1)").attr("imgPath");
							}
							
						})
						if(btnFlag)return false;
						returninfo.refundPrice = $("#refundPrice").val()
						returninfo.refundAccountPrice = $("#refundAccountPrice").val()
						var returnReasonType = $("#returnReasonType").val()
						if (!returnReasonType) {
							$.alert( "请选择申请原因");
							return false
						}
						btnFlag=true;
						$.ajax({
							type: "get",
							url: App.getContextPath() + "admin/returnorderoff/saveReturnOrderOffV2.do",
							data: {
								returnInfo:JSON.stringify(returninfo),returnReason:returnReason,imgPaths:imgPath, returnReasonType: returnReasonType,
								userId:order.userId,expressCompany:expressCompany,expressCode:expressCode
							},
							async: true,
							success: function(result) {
								btnFlag=false;
								if(result.type=='success'){
									window.location.href=App.getContextPath()+"/pages/returnorder/return_order_list.html";
								}else{
 									$.alert(result.content)
								}
								 $(this).attr("disabled",false)
							},
							error: function(e){btnFlag=false;}
						});	
					}
					var checkQuantity = function(tr){
						var tq = $(tr).find('td:eq(5)').find(".returnquantity");
						var quantity = $(tq).val() ;
						var maxValue = $(tq).attr('max') ;
						var skuId = $(tq).attr("skuid");
						if(quantity > maxValue){
							$.alert("退货数量不能大于剩余数量")
							return false
						}
						if(quantity=="" || quantity==0){
							 $("#"+skuId).attr("disabled",true)
							return false
						}else{
							 $("#"+skuId).attr("disabled",false)
						}
						var size  = returninfo.skuInfos.length;
						if(size > 0){
							
							$.each(returninfo.skuInfos ,function(index,item){
								if(skuId=item.skuId){
									item.quantity  = quantity;
								}
							})
							
							// getReturnPrice()
							
						}
							return true;
					}
					
					var getOrderInfo =function(){
						$.ajax({
							type: "get",
							url: App.getContextPath() + "admin/returnorderoff/getOrderInfo.do",
							data: {
								orderId:orderId
							},
							async: true,
							success: function(result) {
								
								order= result.orderInfo;
								
								$("#orderId").html(order.orderId);
								$("#orderStatus").html(getStatusDesc(order.orderStatus));
								$("#totalPrice").html(format_thousands(order.totalPrice));
								$("#hasReturnPrice").html(format_thousands(order.refundPrice));
								$("#hasReturnAccountPrice").html(format_thousands(order.refundAccountPrice));
								
								var isShow ="";
							   $.each(order.detailList,function(index,item){
								   var lastAmount = item.quantity - item.amount;
							     var tr="<tr><td>"+item.productName+"</td>"
							     	+"<td imgpath='"+item.imageUrl+"' skuid='"+item.skuId+"'>"+item.skuCode+"</td>"
							     	+"<td>"+item.shortName+"</td>"
							     	+"<td>"+item.size+"</td>"
							     	+"<td>"+(item.salePrice==null?"":format_thousands(item.salePrice))+"</td>"
								   if(lastAmount<=0){
									   tr+="<td><input class='returnquantity' readonly='"+true+"' style='width:50px' skuid='"+item.skuId+"' max='"+lastAmount+"' value='"+lastAmount+"'/></td>"
									   +"<td>"+"<button type='button' disabled  id='"+item.skuCode+"' orderDetailId='"+ item.orderDetailId + "' class='btn btn-success returngoods' >退货</button>"+"</td></tr>";
								   }else{
									   tr+="<td><input type='text'  class='returnquantity' style='width:50px' skuid='"+item.skuId+"' max='"+lastAmount+"' value='"+lastAmount+"'/></td>"
									   +"<td>"+"<button type='button' id='"+item.skuId+"' orderDetailId='"+ item.orderDetailId + "' class='btn btn-success returngoods' >退货</button>"+"</td></tr>";
								   }
							     $("#source_sku_list tbody").append(tr);
							     
							     
							   /*   $("#source_sku_list tbody tr").find(".returnquantity").unbind("onchange").onchange(function(){
										var quantity = this.value ;
										var maxValue = this.max ;
										var skuId = $(this).attr("skuid");
										console.log(skuId)
										console.log(quantity)
										if(quantity > maxValue){
											$.alert("退货数量不能大于剩余数量")
											return
										}
										if(quantity=="" || quantity==0){
											 $("#"+skuId).attr("disabled",true)
											return
										}else{
											 $("#"+skuId).attr("disabled",false)
										}
										var size  = returninfo.skuInfos.length;
										if(size > 0){
											
											$.each(returninfo.skuInfos ,function(index,item){
												if(skuId=item.skuId){
													item.quantity  = quantity;
												}
											})
											
											getReturnPrice()
										}
								 }); */
							  });
							}
						});
					}
				return {
					init: function() {
						App.init();
						orderId=$.getUrlParam("orderId");	
						returnType=$.getUrlParam("returnType");	
						
						getOrderInfo();
						
						  $("#source_sku_list tbody").on("click",".returngoods",returnGoods); 
						  $("#sku_list tbody").on("click",".cancelReturn",cancelReturn); 
						$(".saveReturn").click(saveReturn);
					}
				}
			}();
			$(function() {
				$("#header").load("../head.html");
				///$("#sidebar").load("../menu.html");
				AddDetail.init();
			});
		</script>

		<!-- /JAVASCRIPTS -->
	</body>

</html>